package com.whp.yky.yky.controller;



import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whp.yky.yky.entity.ShopSysUser;
import com.whp.yky.yky.entity.VoToken;

import com.whp.yky.yky.response.ResponseData;

import com.whp.yky.yky.service.impl.ShopSysUserServiceImpl;
import com.whp.yky.yky.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


@Api(tags = "用户接口")
@CrossOrigin
@RestController
public class UserController {

    @Autowired
    private ShopSysUserServiceImpl userServiceImpl;


    @ApiOperation(value="新增用户", notes="新增用户")
    @PostMapping("/SysUser/insertUser")
    public ResponseData insertUserInfo(@RequestBody ShopSysUser user){



        ResponseData res = new ResponseData();
        boolean result= userServiceImpl.insertUserInfo(user);
        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }

    @DeleteMapping("/SysUser/deletUserByID/{id}")
    @ApiOperation(value="删除用户", notes="根据id删除用户")
    public ResponseData deletUserByID(@PathVariable("id") int id){
        ResponseData res = new ResponseData();
        boolean result = userServiceImpl.deleteUserInfo(id);

        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }

    @ApiOperation(value="修改用户信息", notes="修改用户信息")
    @PostMapping("/SysUser/updateUserInfo")
    public ResponseData updateUserInfoById(@RequestBody ShopSysUser user){


        ResponseData res = new ResponseData();
        boolean result= userServiceImpl.updateUserInfo(user);
        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }


    @ApiOperation(value="获取所有用户信息", notes="获取所有用户信息")
    @PostMapping("/SysUser/getUserInfo")
    public ResponseData getUserInfo(@RequestParam String token){

        ShopSysUser user = new ShopSysUser();
        ResponseData res = new ResponseData();
        try {

            DecodedJWT verify = JwtUtil.verify(token);
//            System.out.println(verify.getClaim("username").asString());
            user.setUsername(verify.getClaim("username").asString());
            res.setCode(20000);
            System.out.println(userServiceImpl.getUser(user));
            res.setData(userServiceImpl.getUser(user));

        }catch (SignatureVerificationException e){
            e.printStackTrace();
        }catch (TokenExpiredException e){
            e.printStackTrace();
        }catch (AlgorithmMismatchException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }




        return res;
    }


    @ApiOperation(value="登录", notes="登录")
    @PostMapping("/SysUser/login")
    public ResponseData login(@RequestBody ShopSysUser user){


        Map<String,String> tk = new HashMap<>();
        tk.put("username",user.getUsername());
        tk.put("password",user.getPassword());
        tk.put("id",String.valueOf(userServiceImpl.getUser(user).getId()));
        ResponseData res = new ResponseData();
        try {
            ShopSysUser vouser = userServiceImpl.login(user);
            if (vouser!=null){
                String token = JwtUtil.getToken(tk);
                res.setCode(20000);
                System.out.println(vouser.getType());
                res.setData(new VoToken(token,user.getUsername()));
            }else{
                res.setCode(500);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return res;


    }

    @ApiOperation(value="登录", notes="登录")
    @PostMapping("/login1")
    public ResponseData login1(@RequestBody ShopSysUser user, HttpServletRequest request, HttpServletResponse response){


//        Map<String,String> tk = new HashMap<>();
//        tk.put("username",user.getUsername());
//        tk.put("password",user.getPassword());
//        tk.put("id",String.valueOf(userServiceImpl.getUser(user).getId()));
        ResponseData res = new ResponseData();

        try {
            ShopSysUser vouser = userServiceImpl.login(user);
            if (vouser!=null){
//                String token = JwtUtil.getToken(tk);
                HttpSession session = request.getSession();
                session.setAttribute("user", vouser);

                res.setCode(20000);
                res.setData(vouser);
            }else{
                res.setCode(500);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return res;


    }

    @ApiOperation(value="登出", notes="登出")
    @PostMapping("/SysUser/logout")
    public ResponseData logout(){



        ResponseData res = new ResponseData();


        res.setCode(20000);
        res.setData("sucess");


        return res;


    }

    @ApiOperation(value="获取所有用户信息", notes="获取所有用户信息")
    @PostMapping("/SysUser/getUserList")
    public ResponseData getUserList(@RequestBody ShopSysUser user, @RequestParam Integer pageNum, @RequestParam Integer pageSize){


        ResponseData res = new ResponseData();
        try {
            res.setCode(20000);
            IPage<ShopSysUser> pageList = this.userServiceImpl.getUserInfo(new Page((long)pageNum, (long)pageSize),user);
            res.setData(pageList);

        }catch (SignatureVerificationException e){
            e.printStackTrace();
        }catch (TokenExpiredException e){
            e.printStackTrace();
        }catch (AlgorithmMismatchException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }




        return res;
    }







}
